package com.nondo.dean.dealer.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.nondo.dean.common.web.View;
import com.nondo.dean.dealer.domain.CustomerView;
import com.nondo.dean.dealer.domain.CustomerViewExample;
import com.nondo.dean.dealer.mapper.CustomerViewExtendsMapper;
import com.nondo.dean.dealer.mapper.CustomerViewMapper;
import com.nondo.dean.dealer.model.result.CustomerResult;
import com.nondo.dean.dealer.model.result.CustomerViewResult;
import com.nondo.dean.dealer.model.result.ProductResult;

/**
 * @author xiaofan
 * @Description: 客源流量业务管理类
 * @throws
 * @date 2018-05-1811:46
 * @Version
 */
@Component
public class CustomerViewService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CustomerViewMapper customerViewMapper;

    @Autowired
    private  ProductService productService;

    @Autowired
    private CustomerViewExtendsMapper customerViewExtendsMapper;
    
    /**
     * 添加
     *
     * @param customerView
     * @return
     */
    @Transactional
    public int add(CustomerView customerView) {
        return this.customerViewMapper.insertSelective(customerView);
    }

    /**
     * 更新
     *
     * @param customerView
     * @return
     */
    @Transactional
    public int update(CustomerView customerView) {
        return this.customerViewMapper.updateByPrimaryKeySelective(customerView);
    }

    /**
     * 删除
     *
     * @param customerView
     * @return
     */
    public int delete(CustomerView customerView) {
        customerView.setDelFlag(CustomerView.DEL_FLAG_DELETE);
        return this.customerViewMapper.updateByPrimaryKeySelective(customerView);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public CustomerView select(Integer id) {
        return this.customerViewMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param customerView
     * @return
     */
    public List<CustomerView> getList(int offset, int limit, CustomerView customerView) {
        CustomerViewExample example = new CustomerViewExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        CustomerViewExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, customerView);
        return customerViewMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 数据统计
     *
     * @param customerView
     * @return
     */
    public Long count(CustomerView customerView) {
        CustomerViewExample example = new CustomerViewExample();
        CustomerViewExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, customerView);
        return customerViewMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param customerView
     */
    void setCriteria(CustomerViewExample.Criteria criteria, CustomerView customerView) {
        if (customerView != null) {
            if (customerView.getId() != null) {
                criteria.andIdEqualTo(customerView.getId());
            }
        }
        criteria.andDelFlagEqualTo(CustomerView.DEL_FLAG_NORMAL);
    }

    /**
     * 查询客户浏览轨迹详情
     * @param customerId
     * @return
     */
    public CustomerResult searchCustomer(Integer customerId){
        CustomerResult result = getCustomer(customerId);
        List<ProductResult> newList = new ArrayList<>();
        List<CustomerViewResult> viewResultList = customerViewExtendsMapper.selectByExample(customerId);
        for(CustomerViewResult beans : viewResultList){
           ProductResult productResult = productService.getProductBeanById(Integer.parseInt(beans.getGoValue().trim()));
           productResult.setViewCount(beans.getCount());
           result.setType(beans.getType());
           newList.add(productResult);
        }
        result.setProductResultList(newList);

        return result;
    }


    /**
     * 获取单个Customer客户
     * @param customerId
     * @return
     */
    public CustomerResult getCustomer(Integer customerId){
        CustomerResult bean = new CustomerResult();
        @SuppressWarnings("rawtypes")
        View resultView = restTemplate.postForEntity("http://dean-platform/platform/v1/inner/customer/select/"+customerId,null, View.class).getBody();
        if(resultView.getCode() == 20000 && resultView.getData() !=null){
            bean= JSON.parseObject(JSON.toJSONString(resultView.getData()),CustomerResult.class);
        }
        return bean;
    }


}
